package com.demo.shardingsphere5.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.demo.shardingsphere5.annotation.ShardingStrategy;
import com.demo.shardingsphere5.dao.domain.User;
import com.demo.shardingsphere5.dao.mapper.UserMapper;
import com.demo.shardingsphere5.service.UserService;
import org.apache.shardingsphere.sharding.algorithm.sharding.hint.HintInlineShardingAlgorithm;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 强制分片，指定sql在特定db执行
 * ShardingSphere5提供强制分片实现类：{@link HintInlineShardingAlgorithm}
 */
@Service
public class ForceHintUserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @ShardingStrategy(shardingValue = "#userId")
    public void insert(Long userId) {
        User user = new User();
        user.setUserName("aaa");
        user.setUserId(userId);

        super.save(user);
    }

    @ShardingStrategy(shardingValue = "#userId")
    public List<User> queryByUserId(Long userId) {
        return super.lambdaQuery().eq(User::getUserId, userId).list();
    }

    @ShardingStrategy(dbIndex = "#dbIndex")
    public List<User> queryByUserId(int dbIndex, Long userId) {
        return super.lambdaQuery().eq(User::getUserId, userId).list();
    }
}
